#! /bin/env python
"""
LinkedList implement
@Author         luciferinux
@Create Time    2024-12-25
"""
class Node:
    """
    单链表的节点
    """
    def __init__(self, item):
        self.item = item
        self.next = None

class SingleLinkedList(object):
    """
    单链表
    """
    def __init__(self):
        self.head = None
        self.length = 0

    def getlength(self):
        return self.length
    
    def isEmpty(self):
        return self.length == 0
    
    def append(self, itemvalue):
        newnode = Node(itemvalue)
        cursor = self.head
        if cursor is not None:
            while cursor.next is not None:
                cursor = cursor.next
            cursor.next = newnode
        else:
            self.head = newnode
        self.length = self.length+1

    def add(self, itemvalue):
        newnode = Node(itemvalue)
        newnode.next = self.head
        self.head = newnode
        self.length = self.length+1

    def item(self):
        cursor = self.head
        while cursor is not None:
            yield cursor.item
            cursor = cursor.next

sll = SingleLinkedList()
sllg = sll.item()
sll.append(5)
for i in range(10):
    sll.append(i)

sll.append("a")
sll.add(3.0)
sll.append(4.5)
for i in sllg:
    print(i)

print(f"链表的长度为{sll.getlength()}")
